package com.jeethink.erp.sale.controller;

import java.util.List;

import com.jeethink.common.core.domain.entity.SysUser;
import com.jeethink.common.core.domain.entity.TbShop;
import com.jeethink.common.core.domain.model.LoginUser;
import com.jeethink.common.utils.ServletUtils;
import com.jeethink.common.utils.spring.SpringUtils;
import com.jeethink.erp.shop.service.ITbShopService;
import com.jeethink.framework.web.service.TokenService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jeethink.common.annotation.Log;
import com.jeethink.common.core.controller.BaseController;
import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.enums.BusinessType;
import com.jeethink.erp.sale.domain.TbSale;
import com.jeethink.erp.sale.service.ITbSaleService;
import com.jeethink.common.utils.poi.ExcelUtil;
import com.jeethink.common.core.page.TableDataInfo;

/**
 * 销售Controller
 * 
 * @author ypr
 * @date 2021-06-07
 */
@RestController
@RequestMapping("/erp/sale")
public class TbSaleController extends BaseController
{
    @Autowired
    private ITbSaleService tbSaleService;

    @Autowired
    private ITbShopService tbShopService;

    /**
     * 查询销售列表
     */
    @PreAuthorize("@ss.hasPermi('erp:sale:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbSale tbSale)
    {
        startPage();
        List<TbSale> list = tbSaleService.selectTbSaleList(tbSale);
        return getDataTable(list);
    }

    /**
     * 导出销售列表
     */
    @PreAuthorize("@ss.hasPermi('erp:sale:export')")
    @Log(title = "销售", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbSale tbSale)
    {
        List<TbSale> list = tbSaleService.selectTbSaleList(tbSale);
        ExcelUtil<TbSale> util = new ExcelUtil<TbSale>(TbSale.class);
        return util.exportExcel(list, "sale");
    }

    /**
     * 获取销售详细信息
     */
    @PreAuthorize("@ss.hasPermi('erp:sale:query')")
    @GetMapping(value = "/{saleId}")
    public AjaxResult getInfo(@PathVariable("saleId") Long saleId)
    {
        return AjaxResult.success(tbSaleService.selectTbSaleById(saleId));
    }

    /**
     * 新增销售
     */
    @Log(title = "销售", businessType = BusinessType.INSERT)
    @PostMapping
    //开启事务
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED,readOnly = false,rollbackFor = {Exception.class})
    public AjaxResult add(@RequestBody TbSale tbSale)
    {
        return tbSaleService.insertTbSale(tbSale);
    }

    /**
     * 修改销售
     */
    @PreAuthorize("@ss.hasPermi('erp:sale:edit')")
    @Log(title = "销售", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbSale tbSale)
    {
        return toAjax(tbSaleService.updateTbSale(tbSale));
    }

    /**
     * 删除销售
     */
    @PreAuthorize("@ss.hasPermi('erp:sale:remove')")
    @Log(title = "销售", businessType = BusinessType.DELETE)
	@DeleteMapping("/{saleIds}")
    public AjaxResult remove(@PathVariable Long[] saleIds)
    {
        return toAjax(tbSaleService.deleteTbSaleByIds(saleIds));
    }



    @GetMapping("/findListSale")
    public TableDataInfo findListSale(TbSale tbSale){
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        //查询如果为一级店铺 则查询所有销售信息
        TbShop tbShop = tbShopService.selectTbShopById(user.getShopId());
        if(tbShop.getParentId() != 0){
            tbSale.setShopId(user.getShopId());
        }

        startPage();
        List<TbSale> list = tbSaleService.findListSale(tbSale);
        return getDataTable(list);
    }

    /***
     * 入库操作
     * @param tbSale
     * @return
     */
    @GetMapping("/deliveryButton")
    public AjaxResult deliveryButton(TbSale tbSale){


        return tbSaleService.updateDelivery(tbSale);
    }

}
